package com.yang.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.common.Meta;
import com.yang.common.R;
import com.yang.common.Result;
import com.yang.pojo.*;
import com.yang.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class SearchController {

    private final String FLAG = "data";
    @Autowired
    private SearchService service;

    /**
     * 取一部分文档进行批量增加到hanchuan索引中，以后的搜索只在此索引中进行
     * @return
     * @throws IOException
     */
    @ResponseBody
    @GetMapping("/search")
    public List<Meta> getAll() throws IOException {
        List<Meta> list = service.getAll();
//        System.out.println(list.get(2));
        return list;
    }

    /**
     * 根据关键字来进行搜索，并进行分页查询
     * @param keyword
     * @return
     */
    @ResponseBody
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")

    public Page searchByMsg(@PathVariable String keyword,
                                  @PathVariable int pageNo,
                                  @PathVariable int pageSize) throws IOException {
        Result result = service.searchAllHighLight(keyword,pageNo,pageSize);
//        System.out.println("结果集size" + list.size());
        Page<Meta> page = new Page<>(pageNo,pageSize);
        page.setRecords(result.getList());
        page.setTotal(result.getTotal());
        return page;
    }

    @Autowired
    private DefectService defectService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private RiskControllerService riskControllerService;
    @Autowired
    private SecurityService securityService;
    @Autowired
    private WorkticketService workticketService;

    /**
     * 根据传过来的id以及数据库表名来进行查看详情
     * @param id
     * @param map
     * @return
     */

//    @RequestMapping("/details/{id}")
//    public String look_details(@PathVariable("id") String id, Map<String,Object> map){
////        System.out.println("过来了" + e);
//        String[] str = id.split("001_");
//
//        if (str[0].equals("defect")){
//            Defect defect = defectService.getById(str[1]);
//            map.put(FLAG,defect);
//        } else if (str[0].equals("device")) {
//            Device device = deviceService.getById(str[1]);
//            map.put(FLAG,device);
//        } else if (str[0].equals("riskcontroller")) {
//            Riskcontroller riskcontroller = riskControllerService.getById(str[1]);
//            map.put(FLAG,riskcontroller);
//        }else if (str[0].equals("security")) {
//            Security security = securityService.getById(str[1]);
//            map.put(FLAG,security);
//        }else if (str[0].equals("workticket")) {
//            Workticket workticket = workticketService.getById(str[1]);
//            map.put(FLAG,workticket);
//        }
//        System.out.println("map的size " + map.size());
//        System.out.println("map的内容 " + map.toString());
//        return "details";
//    }

    @RequestMapping("/details/{id}")
    @ResponseBody
    public R look_details2(@PathVariable("id") String id, Map<String,Object> map){
//        System.out.println("过来了" + e);
        String[] str = id.split("001_");

        if (str[0].equals("defect")){
            Defect defect = defectService.getById(str[1]);
//            map.put(FLAG,defect);
            return R.success(defect);
        } else if (str[0].equals("device")) {
            Device device = deviceService.getById(str[1]);
//            map.put(FLAG,device);
            return R.success(device);
        } else if (str[0].equals("riskcontroller")) {
            Riskcontroller riskcontroller = riskControllerService.getById(str[1]);
//            map.put(FLAG,riskcontroller);
            return R.success(riskcontroller);
        }else if (str[0].equals("security")) {
            Security security = securityService.getById(str[1]);
//            map.put(FLAG,security);
            return R.success(security);
        }else if (str[0].equals("workticket")) {
            Workticket workticket = workticketService.getById(str[1]);
//            map.put(FLAG,workticket);
            return R.success(workticket);
        }
        return R.error("查询失败");
    }
}
